package com.orangelife.service.propFeeRuleLinkApartment.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeRuleLinkApartment.IPropFeeRuleLinkApartmentService;
import com.orangelife.service.propFeeRuleLinkApartment.model.PropFeeRuleLinkApartmentDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeRuleLinkApartment类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 10:45:42
 * ==============================================================
 */
@Service("propFeeRuleLinkApartmentService")
public class PropFeeRuleLinkApartmentServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeeRuleLinkApartmentService {
	private static Logger logger = Logger
			.getLogger(PropFeeRuleLinkApartmentServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeRuleLinkApartmentServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeRuleLinkApartment(
			PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment)
			throws AppBizException {

		Assert.notNull(propFeeRuleLinkApartment);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
		propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
		propFeeRuleLinkApartment.setTscreate(new Date());
		propFeeRuleLinkApartment.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeRuleLinkApartmentMapper.insertSelective",
				propFeeRuleLinkApartment);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeRuleLinkApartment(
			PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment)
			throws AppBizException {

		Assert.notNull(propFeeRuleLinkApartment);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeRuleLinkApartment.getLinkID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeRuleLinkApartmentDto oldPropFeeRuleLinkApartment = this
				.findPropFeeRuleLinkApartmentByID(propFeeRuleLinkApartment
						.getLinkID());

		if (oldPropFeeRuleLinkApartment == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeRuleLinkApartment.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleLinkApartmentMapper.updateByPrimaryKeySelective",
				propFeeRuleLinkApartment);
	}

	@Override
	public PropFeeRuleLinkApartmentDto findPropFeeRuleLinkApartmentByID(
			String propFeeRuleLinkApartmentID) throws AppBizException {
		if (StringUtils.isBlank(propFeeRuleLinkApartmentID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleLinkApartmentDto oldPropFeeRuleLinkApartment = this
				.getSqlSession()
				.selectOne(
						"PropFeeRuleLinkApartmentSearchMapper.selectByPrimaryKey",
						propFeeRuleLinkApartmentID);
		if (oldPropFeeRuleLinkApartment == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF
				.equals(oldPropFeeRuleLinkApartment.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeRuleLinkApartment;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeRuleLinkApartmentByLogic(
			String propFeeRuleLinkApartmentID) throws AppBizException {

		if (StringUtils.isBlank(propFeeRuleLinkApartmentID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleLinkApartmentDto oldPropFeeRuleLinkApartment = this
				.findPropFeeRuleLinkApartmentByID(propFeeRuleLinkApartmentID);

		oldPropFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeRuleLinkApartment.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleLinkApartmentMapper.updateByPrimaryKeySelective",
				oldPropFeeRuleLinkApartment);
	}

	@Override
	public List<Map<String, Object>> findPropFeeRuleLinkApartmentByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.findAllPropFeeRuleLinkApartment",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkApartmentDto> getPropFeeRuleLinkApartmentByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeRuleLinkApartmentDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.getAllPropFeeRuleLinkApartmentDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeRuleLinkApartmentByRelevantID(
			String linkID, String ruleID, String commID, String apartmentID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID", linkID);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.selectPropFeeRuleLinkApartmentDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkApartmentDto> findPropFeeRuleLinkApartmentByRelevantID(
			String linkID, String ruleID, String commID, String apartmentID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID", linkID);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<PropFeeRuleLinkApartmentDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.findPropFeeRuleLinkApartmentDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkApartmentDto> bauthfindPropFeeRuleLinkApartment(
			List<String> propFeeRuleLinkApartmentIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeRuleLinkApartmentDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.batchFindPropFeeRuleLinkApartment",
						propFeeRuleLinkApartmentIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeRuleLinkApartmentByMap(
			Map<String, Object> propFeeRuleLinkApartment)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkApartmentSearchMapper.batchFindPropFeeRuleLinkApartmentByMap",
						propFeeRuleLinkApartment);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public List<Map<String, Object>> findRuleHouseInfo(String linkID,
			String ruleID, String commID, String feeTypeID, String apartmentID,
			List<String> apartmentIDs,String validityType,String ctype,String ruleType,String startTime,String endTime) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID", linkID);
		map.put("ruleID", ruleID);
		map.put("commID", commID);

		map.put("feeTypeID", feeTypeID);
		map.put("ctype", ctype);
		map.put("ruleType", ruleType);
		map.put("apartmentID", apartmentID);
		map.put("apartmentIDs", apartmentIDs);
		map.put("validityType", validityType);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeRuleLinkApartmentSearchMapper.findRuleHouseInfo", map);
		return PropertyUtils.turnToEntity(list);
	}
}
